using System;
using MSScriptControl;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.ManagedCode;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	/// <summary>
	/// Summary description for ScriptCodeOnVersionUpgradeWorker.
	/// </summary>
	class ScriptCodeOnVersionUpgradeWorker
	{
		
		public static void OnVersionUpgrade(Form form, VersionCode docVersion)
		{
			ScriptSource scriptSource = form.Manifest.ScriptSource;
			if( scriptSource == null )
			{
				return;
			}

			try
			{
				if( form.ScriptControlContext == null )
				{
					ScriptControlContext.buildContext( scriptSource, form );
				}

				XDocument xdoc = form.ScriptControlContext.XDocument;
				Application app = form.ScriptControlContext.Application;

				VersionUpgradeEvent upgradeEvent = new VersionUpgradeEvent( xdoc, form, docVersion );

				ScriptControl scriptControl = form.ScriptControlContext.ScriptControl;

				foreach( Procedure proc in scriptControl.Procedures )
				{
					if( proc.Name.Equals( "XDocument_OnVersionUpgrade" ) )
					{
						object[] param = new object[]{ upgradeEvent };
						scriptControl.Run( "XDocument_OnVersionUpgrade", ref param );
                        break;
					}
				}
			}
			catch( Exception e )
			{
                FormUtil.addInnerExceptionMassage(form, "XDocument_OnVersionUpgrade", e);
			}
		}

	}
}
